package com.ruipeng.shengyapo.model;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * 裁剪图片
 * 
 * @author Damon
 *
 */
public class CropImage {
	/**
	 * 源图片地址
	 */
	private String srcPath;
	/**
	 * 目标图片地址
	 */
	private String subPath;
	/**
	 * 缩放后图片大小
	 */
	private int imgW;
	/**
	 * 缩放后图片大小
	 */
	private int imgH;
	/**
	 * 
	 */
	private int imgX1;
	/**
	 * 
	 */
	private int imgY1;
	/**
	 * 裁剪后图片大小
	 */
	private int cropW;
	/**
	 * 裁剪后图片大小
	 * 
	 */
	private int cropH;

	public void cut() {
		BufferedImage bi;
		try {
			// 读取源图像
			bi= ImageIO.read(new File(srcPath));
			Image image = bi.getScaledInstance(imgW, imgH, Image.SCALE_AREA_AVERAGING);// 获取缩放后图片大小
			ImageFilter cropFilter = new CropImageFilter(imgX1, imgY1, cropW, cropH);
			Image img = Toolkit.getDefaultToolkit().createImage(
					new FilteredImageSource(image.getSource(), cropFilter));
			BufferedImage tag = new BufferedImage(cropW, cropH,
					BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(img, 0, 0, null); // 绘制截取后的图
			g.dispose();
            File cropFile=new File(subPath);
            if(!cropFile.exists()){
            	cropFile.mkdirs();
            }
			ImageIO.write(tag, getExtention(srcPath), new File(subPath));// 输出为文件
			// 再次进行缩放
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 功能：提取文件名的后缀
	 * 
	 *
	 * 
	 * @param fileName
	 * 
	 * @return
	 */

	private static String getExtention(String fileName) {

		int pos = fileName.lastIndexOf(".");

		return fileName.substring(pos + 1);

	}

	public CropImage(int imgW, int imgH, int imgX1, int imgY1, int cropW,
			int cropH) {
		super();
		this.imgW = imgW;
		this.imgH = imgH;
		this.imgX1 = imgX1;
		this.imgY1 = imgY1;
		this.cropW = cropW;
		this.cropH = cropH;
	}

	public String getSrcPath() {
		return srcPath;
	}

	public void setSrcPath(String srcPath) {
		this.srcPath = srcPath;
	}

	public String getSubPath() {
		return subPath;
	}

	public void setSubPath(String subPath) {
		this.subPath = subPath;
	}

	public int getImgW() {
		return imgW;
	}

	public void setImgW(int imgW) {
		this.imgW = imgW;
	}

	public int getImgH() {
		return imgH;
	}

	public void setImgH(int imgH) {
		this.imgH = imgH;
	}

	public int getImgX1() {
		return imgX1;
	}

	public void setImgX1(int imgX1) {
		this.imgX1 = imgX1;
	}

	public int getImgY1() {
		return imgY1;
	}

	public void setImgY1(int imgY1) {
		this.imgY1 = imgY1;
	}

	public int getCropW() {
		return cropW;
	}

	public void setCropW(int cropW) {
		this.cropW = cropW;
	}

	public int getCropH() {
		return cropH;
	}

	public void setCropH(int cropH) {
		this.cropH = cropH;
	}
}
